package com.example.front.mapper;

import com.example.common.dto.Pagination;
import com.example.front.dto.EventSearchDto;
import com.example.front.entity.Event;
import com.example.passport.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface EventMapper {

    @Select("SELECT * FROM `event` WHERE `tenant_id` = #{event.tenantId} AND `name` = #{event.name} AND `status`!=" + User.STATUS_DELETE)
    Event findByName(@Param("event") Event event);

    @Select("SELECT * FROM `event` WHERE `tenant_id` = #{tenantId} AND `id` = #{id} AND `status`!=" + User.STATUS_DELETE)
    Event findById(@Param("tenantId") long tenantId,@Param("id") Long id);

    @Select("<script>" +
            "SELECT count(*) FROM `event`" +
            "<where>" +
            "`tenant_id`=#{event.tenantId}" +
            "<if test='event.id!=null'> AND `id`!=#{event.id}</if>" +
            "<if test='event.name!=null'> AND `name`=#{event.name}</if>" +
            "</where> AND `status`!=" + User.STATUS_DELETE +
            "</script>")
    long count(@Param("event") EventSearchDto event);

    @Select("<script>" +
            "SELECT * FROM `event`" +
            "<where>" +
            "`tenant_id`=#{event.tenantId}" +
            "<if test='event.id!=null'> AND `id`!=#{event.id}</if>" +
            "<if test='event.name!=null'> AND `name`=#{event.name}</if>" +
            "</where> AND `status`!=" + User.STATUS_DELETE +
            " ORDER BY `created_at` DESC LIMIT #{pagination.offset}, #{pagination.limit}" +
            "</script>")
    List<Event> findAll(@Param("event") Event event, @Param("pagination") Pagination pagination);

    @Insert("INSERT INTO `event` (`tenant_id`, `name`) VALUES (#{event.tenantId}, #{event.name})")
    int create(@Param("event") Event event);

    @Update("UPDATE `event` SET `status` ="+ User.STATUS_DELETE+
            " WHERE `id`=#{event.id} AND `tenant_id`=#{event.tenantId} AND `status`!=" + User.STATUS_DELETE)
    int delete(@Param("event") Event event);

    @Update("UPDATE `event` SET `name` = #{name}" +
            " WHERE `tenant_id` = #{tenantId} AND `id` = #{id} AND `status`!=" + User.STATUS_DELETE)
    int update(Event event);
}
